<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="1"
        :formType="type"
        labelWidth="110"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <AjaxButton :click="saveDialog" type="primary">确 定</AjaxButton>
      <el-button @click="closeDialog">返 回</el-button>
    </div>
  </div>
</template>

<script>
import {
  driveCancel,
 } from '@/api/potenCus/customer/driveManagement';
import { dictionary } from '@/utils/dictMixin';


export default {
  name: 'EntryMileage',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'mileage'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
    };
  },
  watch: {
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      if (this.type === 'mileage' ) {
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '开始里程',
          fieldName: 'driveSKm',
          placeholder: '开始里程',
          style: { width: '200px' },
          disabled: true
        },
        {
          type: 'INPUT',
          label: '结束里程',
          fieldName: 'driveEKm',
          placeholder: '请输入结束里程',
          style: { width: '200px' },
          rules: [{ required: true, message: '请输入结束里程', trigger: 'blur' }],
          unitRender: () => {
            return <span>KM</span>
          },
          change: val => {
            this.changeCancel(val);
          }
        },
        {
          type: 'INPUT',
          label: '本次行驶',
          fieldName: 'driveRunKm',
          placeholder: '本次行驶',
          style: { width: '200px' },
          readonly: true
        },
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    changeCancel(val){
      console.log('结束里程', val);
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let res = {};
        if (this.type === 'mileage') {
          if(this.saveData.driveEKm > this.rowData.driveSKm){
            res = await driveCancel({ ...this.saveData, itemId: this.rowData.itemId, planStatus: 20211003 });
            // console.log('录入里程', res);
          } else {
            this.$message.info("结束里程不得小于开始里程!");
          }
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        }
      }
    },

    closeDialog(param) {
      this.$emit('close', false, param);
    },
    // async getCancelReason() {
    //   const res = await cancelReason();
    //   console.log('取消试驾原因', res)
    //   if (res.resultCode === 200) {
    //     this.formList[0].itemList = res.data.map(x => ({ value: x.cancelCauseId, text: x.cancelExplain }));
    //   }
    // },
  }
};
</script>

<style lang="less" scoped>
</style>

